package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import Entity.*;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;

import javax.swing.JTable;

/**
 * this is the GUI for complaints status report. the conpany managar and the parking lot manager will choose the complaint from the table and view them.
 * @author Shahar Shelly
 *
 */
public class ComplaintStatusReportCreateWin extends JFrame {

	private JPanel contentPane;
	private JTextField startDate;
	private JTextField endDate;
	private JTable table;
	private JTextField ComplaintNum;
	private JTextField CustomerID;
	private JTextField WorkerID;
	private JTextField answer;
	private JTextField comp;
	/**
	 * is the constructor. it has lister to the table and pour the data for making the reports or viewing them.
	 */
	public ComplaintStatusReportCreateWin(ComplaintReport c) {
		final ComplaintReport c2=c;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 760, 326);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setLayout(null);
		
		final JLabel lblOrderReport = new JLabel("Complaints Stauts Report");
		lblOrderReport.setBounds(175, 11, 161, 14);
		contentPane.add(lblOrderReport);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 33, 98, 43);
		panel.setBorder(new TitledBorder(null, "Start Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel);
		panel.setLayout(null);
		
		startDate = new JTextField();
		startDate.setBounds(6, 16, 86, 20);
		panel.add(startDate);
		startDate.setColumns(10);
		startDate.setText(c.getStart().getDate()+"/"+(c.getStart().getMonth()+1)+"/"+(c.getStart().getYear()+1900));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 77, 98, 43);
		panel_1.setBorder(new TitledBorder(null, "End Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		endDate = new JTextField();
		endDate.setBounds(6, 16, 86, 20);
		panel_1.add(endDate);
		endDate.setColumns(10);
		endDate.setText(c.getEndDate().getDate()+"/"+(c.getEndDate().getMonth()+1)+"/"+(c.getEndDate().getYear()+1900));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 131, 180, 108);
		panel_2.setBorder(new TitledBorder(null, "Description", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		final JTextPane textPane = new JTextPane();
		textPane.setBounds(6, 16, 164, 81);
		panel_2.add(textPane);
		
		JButton btnSend = new JButton("Send");
		btnSend.setBounds(20, 251, 89, 23);
		btnSend.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				c2.SetDescription(textPane.getText());
				JOptionPane.showMessageDialog(null,"Your Report was sent to the Company Manager","Sent Successfully",JOptionPane.INFORMATION_MESSAGE);
				ComplaintReport.save(c2);
			}
		});
		contentPane.add(btnSend);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(129, 251, 89, 23);
		contentPane.add(btnCancel);
		
		//Complaint a=new Complaint("314","hi"); a.setHandled(true); a.setWorker_id("1"); a.setResult("kaka"); a.setCompensation(5.5555); a.setDateOfAnswer(new Date());
		//Complaint b=new Complaint("456","bye"); b.setHandled(true); b.setWorker_id("2"); b.setResult("balo"); b.setCompensation(4.567); b.setDateOfAnswer(new Date());
		//final Complaint[] carr={a,b};
		table = new JTable(new MyTable(c.getComplaints()));
		table.setBounds(270, 64, 241, 124);
		JScrollPane sp=new JScrollPane(table);
		sp.setBounds(218, 36, 334, 203);
		contentPane.add(sp);
		
		JLabel lblNumberOfComplaints = new JLabel("Number of Complaints:");
		lblNumberOfComplaints.setBounds(266, 255, 123, 14);
		contentPane.add(lblNumberOfComplaints);
		
		ComplaintNum = new JTextField();
		ComplaintNum.setBounds(388, 252, 40, 20);
		contentPane.add(ComplaintNum);
		ComplaintNum.setColumns(10);
		ComplaintNum.setText(String.valueOf(c.getNumOfComplaint()));
		
		JLabel lblCustomerId = new JLabel("Customer ID:");
		lblCustomerId.setBounds(572, 33, 81, 14);
		contentPane.add(lblCustomerId);
		
		JLabel lblWorkerId = new JLabel("Worker ID:");
		lblWorkerId.setBounds(572, 62, 81, 14);
		contentPane.add(lblWorkerId);
		
		CustomerID = new JTextField();
		CustomerID.setBounds(648, 30, 86, 20);
		contentPane.add(CustomerID);
		CustomerID.setColumns(10);
		
		WorkerID = new JTextField();
		WorkerID.setBounds(648, 56, 86, 20);
		contentPane.add(WorkerID);
		WorkerID.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Result:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setBounds(572, 84, 168, 92);
		contentPane.add(panel_3);
		panel_3.setLayout(null);
		
		final JTextPane textPane_1 = new JTextPane();
		textPane_1.setBounds(6, 16, 150, 69);
		panel_3.add(textPane_1);
		
		JLabel lblAnswerDate = new JLabel("Answer Date:");
		lblAnswerDate.setBounds(572, 188, 81, 14);
		contentPane.add(lblAnswerDate);
		
		answer = new JTextField();
		answer.setBounds(648, 185, 86, 20);
		contentPane.add(answer);
		answer.setColumns(10);
		
		JLabel lblCompensation = new JLabel("Compensation:");
		lblCompensation.setBounds(572, 213, 81, 14);
		contentPane.add(lblCompensation);
		
		comp = new JTextField();
		comp.setBounds(648, 210, 86, 20);
		contentPane.add(comp);
		comp.setColumns(10);
		
		table.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
			    if (e.getClickCount() == 2) {
			      JTable target = (JTable)e.getSource();
			      int row = target.getSelectedRow();
			      CustomerID.setText(c2.getComplaints()[row].getCustomer_id());
			      WorkerID.setText(c2.getComplaints()[row].getWorker_id());
			      textPane_1.setText(c2.getComplaints()[row].getResult());
			      answer.setText(c2.getComplaints()[row].getDateOfAnswer().getDate()+"/"+(c2.getComplaints()[row].getDateOfAnswer().getMonth()+1)+"/"+(c2.getComplaints()[row].getDateOfAnswer().getYear()+1900));
			      comp.setText(String.valueOf(c2.getComplaints()[row].getCompensation()));
			    }
			  }
		});
	}
}

class MyTable extends AbstractTableModel
{
	String[] titles={"Complaint ID","Status","Submisson Date"};
	Object[][] data;
	
	public MyTable(Complaint[] c)
	{
		super();
		this.data=new Object[c.length][3];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][0]=c[i].getComplaint_number();
			if(c[i].isHandled())
				this.data[i][1]="Handled";
			else
				this.data[i][1]="Not Handled";
			this.data[i][2]=c[i].getDateOfSubmition();
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}
